package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ShoppingCartListBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.RefreshGouWuCarEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/18.
 */

public class ShoppingCartViewPagerFragment extends BaseViewPagerFragment {
	private String mDemanId;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mConfirmBtn.setVisibility(View.INVISIBLE);
		mCustomScroll.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
	}

	@Override
	public void onResponse(String response, int what) {
		if (what == 1) {
			ShoppingCartListBean bean = JSON.parseObject(response, ShoppingCartListBean.class);
			if (StringUtils.isNoneNullString(mDemanId)) {
				bean.setDemanId(mDemanId);
			}
			if (bean != null && bean.getList() != null && bean.getList().size() > 0) {
				setData(bean, bean.getList().size(), 6, ShoppingCartFragment.class.getName());
			} else {
				mStateLayout.showEmptyView(true);
				mStateLayout.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}
		}

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ShoppingCartViewPagerFragment) {
			mActivity.setTitle(R.string.shopping_cart_title);
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID) != null) {
			mDemanId = getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID);
			Map<String, Object> paramsMap = new HashMap<>();
			paramsMap.put("demandId", mDemanId);
			paramsMap.put("page", 1);
			paramsMap.put("size", 99999);
			sendRequest(ContractConstant.SHOPPING_CART_LIST, paramsMap, 1);
		}


	}

	public void onEventMainThread(RefreshGouWuCarEvent event) {
		if (peekFragment() instanceof ShoppingCartViewPagerFragment) {
			if (StringUtils.isNoneNullString(event.getRefresh())) {
				requestDatas();
			}
		}

	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (peekFragment() instanceof  ShoppingCartViewPagerFragment && backListenerEvent.getMsg().equals("ShoppingCartViewPagerFragment")) {
			popFragment();
//			//刷新浏览商品
//			Intent intent = new Intent("refreshOrdersList");
//			mActivity.sendBroadcast(intent);
		}
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
